//
//  VistorView.swift
//  weibo_swift
//
//  Created by dulinlong on 15/6/24.
//  Copyright © 2015年 sheji_56. All rights reserved.
//

import UIKit

protocol VistorViewDelegate: NSObjectProtocol{
    func registerButtonClick()
    func loginButtonClick()
}

class VistorView: UIView {

    weak var delegate: VistorViewDelegate?
 
    
    ///  设置视图内容
    ///
    ///  :param: iconName 图标名称
    ///  :param: introStr 描述文本
    ///  :param: isMain   是否是首页
    func setupVistorViewInfo(iconName: String,introStr: String,isMain: Bool){
        
        houseImageView.image = UIImage(named: iconName)
        introLabel.text = introStr

        cycleImageView.hidden = !isMain
        //蒙层图片的位置调整
        isMain ? insertSubview(maskImageView, aboveSubview: cycleImageView):
            sendSubviewToBack(maskImageView)
        
    }
    
    func startAnimation(){
        if cycleImageView.layer.animationForKey("anim1") != nil{
            return
        }
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = M_PI * 2
        anim.duration = 24
        
        anim.fillMode = kCAFillModeForwards
        anim.removedOnCompletion = false//不移除
        
        cycleImageView.layer.addAnimation(anim, forKey: "anim1")
    }
 
    
   override init(frame: CGRect) {
        super.init(frame: frame)
    
        setupAddSubViews()
        setupAutoLayout()
        
    }

   required init(coder aDecoder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }
    
    
    ///  添加自动布局
   private func setupAutoLayout(){
        
        cycleImageView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: cycleImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: cycleImageView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -40))
        
        houseImageView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: houseImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: cycleImageView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: houseImageView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: cycleImageView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        introLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[view1]-20-[view2(40)]", options: NSLayoutFormatOptions.AlignAllCenterX, metrics: nil, views: ["view1":cycleImageView,"view2":introLabel]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[view1(240)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["view1":introLabel]))
        
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[subView(70)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView":registerButton]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[view1]-10-[view2(40)]", options: NSLayoutFormatOptions.AlignAllLeading, metrics: nil, views: ["view1":introLabel,"view2":registerButton]))
        
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[subView(70)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView":loginButton]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[view1]-10-[view2(40)]", options: NSLayoutFormatOptions.AlignAllTrailing, metrics: nil, views: ["view1":introLabel,"view2":loginButton]))
        
        //蒙层的布局
        maskImageView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[view1]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["view1":maskImageView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[view1]-190-|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["view1":maskImageView]))
        
        //用sb中的吸色笔吸出的颜色
        backgroundColor = UIColor(white: 0.93, alpha: 1)

    }
    
    ///  添加子控件
   private func setupAddSubViews(){
        addSubview(cycleImageView)
        addSubview(maskImageView)
        addSubview(houseImageView)
        addSubview(introLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
    }
    
    /// 房子背景图标
    lazy var cycleImageView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return imageView
    }()
    
    /// 蒙层
    lazy var maskImageView: UIImageView = {
       let imageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return imageView
    }()
    
    /// 房子
    lazy var houseImageView: UIImageView = {
       let imageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        
        return imageView
    }()
    
    /// 提示的文字
    lazy var introLabel: UILabel = {
       let label = UILabel(frame: CGRectMake(0, 0, 224, 40))
        label.text = "关注一些人，回这里看看有什么惊喜"
        label.preferredMaxLayoutWidth = 224
        label.numberOfLines = 2
        //label.adjustsFontSizeToFitWidth = true
        label.font = UIFont.systemFontOfSize(14)
        label.textAlignment = NSTextAlignment.Center
        label.textColor = UIColor.darkGrayColor()
        
        label.sizeToFit()
        return label
    }()
    
    /// 注册按钮
    lazy var registerButton: UIButton = {
        let btn = UIButton(type: UIButtonType.Custom)
        btn.frame = CGRectMake(0, 0, 100, 40)
        
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        //btn.adjustsImageWhenHighlighted = false
        
        btn.addTarget(self, action: "registerButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    
    /// 登录按钮
    lazy var loginButton: UIButton = {
        let btn = UIButton(frame: CGRectMake(100, 0, 100, 40))
        
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        //btn.adjustsImageWhenHighlighted = false
        
        btn.addTarget(self, action: "loginButtonClick", forControlEvents: UIControlEvents.TouchUpInside)

        return btn
        }()
    
    //MARK: -注册点击
    func registerButtonClick(){
        delegate?.registerButtonClick()
    }
    //MARK: -登录点击
    func loginButtonClick(){
        delegate?.loginButtonClick()
    }
}
